package com.miukoo.aikao.qpaper.choose;

import com.miukoo.aikao.core.choose.JoinTypeInterface;
import com.miukoo.aikao.core.dtos.ExamDto;
import com.miukoo.aikao.core.enums.QuestionType;
import com.miukoo.aikao.core.exceptions.ApiException;
import com.miukoo.aikao.core.pojos.QuestionChooseDto;
import com.miukoo.aikao.core.types.TypeInterfaceManager;
import com.miukoo.aikao.qexam.api.v1.ExamRepoApi;
import com.miukoo.aikao.qexam.dto.ExamRepoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自由题库组题
 * @Author miukoo<miukoo@126.com>
 * @Date 2020/11/26  
 * @Version 1.0
 */
@Component
public class FreeJoinType implements JoinTypeInterface {

    @Autowired
    TypeInterfaceManager  typeInterfaceManager;
    @Autowired
    private ExamRepoApi examRepoApi;

    @Override
    public List<QuestionChooseDto> chooseQuestion(ExamDto dto) {
        // 查出指定的题库
        List<ExamRepoDto> list = examRepoApi.listByExam(dto.getId());
        if(list!=null&&list.size()>0){
            List<QuestionChooseDto> questions = new ArrayList<>();
            list.forEach(repo->{
                Long repoId = Long.valueOf(repo.getRepoId());
                if(repo.getRadioCount()!=null){
                    List<QuestionChooseDto> temp = typeInterfaceManager.chooseQuestion(repoId, QuestionType.RADIO.shortValue(), dto.getLevel(), repo.getRadioCount(), repo.getRadioMaxOption(), repo.getRadioScore());
                    questions.addAll(temp);
                }
                if(repo.getMultiCount()!=null){
                    List<QuestionChooseDto> temp = typeInterfaceManager.chooseQuestion(repoId, QuestionType.MULTI.shortValue(), dto.getLevel(), repo.getMultiCount(), repo.getMultiMaxOption(), repo.getMultiScore());
                    questions.addAll(temp);
                }
                if(repo.getJudgeCount()!=null){
                    List<QuestionChooseDto> temp = typeInterfaceManager.chooseQuestion(repoId, QuestionType.JUDGE.shortValue(), dto.getLevel(), repo.getJudgeCount(), Short.valueOf("2"), repo.getJudgeScore());
                    questions.addAll(temp);
                }
                if(repo.getQaCount()!=null){
                    List<QuestionChooseDto> temp = typeInterfaceManager.chooseQuestion(repoId, QuestionType.QA.shortValue(), dto.getLevel(), repo.getQaCount(), Short.valueOf("1"), repo.getQaScore());
                    questions.addAll(temp);
                }
                if(repo.getCodingCount()!=null){
                    List<QuestionChooseDto> temp = typeInterfaceManager.chooseQuestion(repoId, QuestionType.CODING.shortValue(), dto.getLevel(), repo.getCodingCount(), Short.valueOf("1"), repo.getCodingScore());
                    questions.addAll(temp);
                }
            });
            return questions;
        }else{
            throw new ApiException(1,"未找到指定的组题题库！");
        }
    }


}
